package com.fzu.express.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fzu.express.annotation.SystemLog;
import com.fzu.express.entity.Order;
import com.fzu.express.entity.WebUser;
import com.fzu.express.pojo.DashboardVO;
import com.fzu.express.pojo.OrderDetail;
import com.fzu.express.service.OrderService;
import com.fzu.express.utils.JwtUtil;
import com.fzu.express.utils.PageVO;
import com.fzu.express.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2022-10-27
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 数据大屏
     * @return R
     */
    @GetMapping("/dashboard")
    public R dashboard(@RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        DashboardVO vo = orderService.dashboard(user);
        return R.ok().data(vo);
    }

    /**
     * 分页条件查询
     * @param order 参数
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return R
     */
    @GetMapping
    public R list(Order order, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        PageVO pageVO = orderService.listOrder(order, user, pageNum, pageSize, true);
        return R.ok().data(pageVO);
    }

    /**
     * 分页条件查询
     * @param order 参数
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return R
     */
    @GetMapping("/listByCourier")
    public R listByCourier(Order order, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        PageVO pageVO = orderService.listOrderByCourier(order, user, pageNum, pageSize);
        return R.ok().data(pageVO);
    }

    /**
     * 分页条件查询
     * @param order 参数
     * @param pageNum 页码
     * @param pageSize 每页记录数
     * @return R
     */
    @GetMapping("/listReceive")
    public R listReceive(Order order, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                  @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        PageVO pageVO = orderService.listOrder(order, user, pageNum, pageSize, false);
        return R.ok().data(pageVO);
    }


    /**
     * 获取订单详情
     * @param id 订单ID
     * @return R
     */
    @GetMapping("/detail")
    public R list(@RequestParam Long id) {
        OrderDetail orderDetail = orderService.getOrderDetail(id);
        return R.ok().data(orderDetail);
    }

    /**
     * 新增订单
     * @param order 参数
     * @return R
     */
    @PostMapping
    @SystemLog(businessName = "新增订单")
    public R add(@RequestBody Order order, @RequestHeader("token") String token) {
        order.setSenderId(JwtUtil.getUser(token).getId());
        orderService.addOrder(order);
        return R.ok();
    }

    /**
     * 订单操作
     * @param order 参数
     * @return R
     */
    @PutMapping("/operate")
    @SystemLog(businessName = "执行订单操作")
    public R operate(@RequestBody Order order, @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        orderService.operate(order, user);
        return R.ok();
    }

    /**
     * 批量执行订单操作
     * @param status 参数
     * @return R
     */
    @PutMapping("/batch-operate/{status}")
    @SystemLog(businessName = "批量执行订单操作")
    public R batchOperate(@PathVariable("status") Integer status, @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        Long res = orderService.batchOperate(status, user);
        return R.ok().data(res);
    }


    /**
     * 用户拒收快件
     * @param id 订单ID
     * @return R
     */
    @PutMapping("/refuse/{id}")
    @SystemLog(businessName = "用户拒收快件")
    public R operate(@PathVariable("id") Long id, @RequestHeader("token") String token) {
        WebUser user = JwtUtil.getUser(token);
        orderService.refuse(id, user);
        return R.ok();
    }



    /**
     * 修改
     * @param order 参数
     * @return R
     */
    @PutMapping
    @SystemLog(businessName = "更新订单")
    public R update(@RequestBody Order order) {
        orderService.updateOrder(order);
        return R.ok();
    }


    /**
     * 删除
     * @param id 订单ID
     * @return R
     */
    @DeleteMapping("/{id}")
    @SystemLog(businessName = "删除订单")
    public R delete(@PathVariable("id") Long id) {
        orderService.deleteOrder(id);
        return R.ok();
    }
}

